package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _135Powxn
 * @description
 * @date 2023/08/24 17:21
 * <p>
 * ******************************************************
 */
public class _135Powxn {

    public double myPow(double x, int n) {
        if (n == 0) return 1.0;
        if (n == 1) return x;
        if (n > 0) {
            int i = n % 2;
            if (i == 1) {
                return x * myPow(x * x, n / 2);
            } else {
                return myPow(x * x, n / 2);
            }
        } else {
            int i = n % 2;
            if (i == -1) {
                return 1 / (x * myPow(x * x, -(n / 2)));
            } else {
                return 1 / myPow(x * x, -(n / 2));
            }
        }
    }
}
